<?php


namespace app\common\helper;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
use think\helper\Str;
use app\common\model\SmsLog as SmsLogModel;
use think\View;

class SendEmailHelper
{
    // 用户名
    protected $username;
    protected $body; // 邮件正文
    protected $subject; // 邮件标题
    protected $email; // 邮箱
    protected $code; // 验证码
    protected $flag; // 随机串 防攻击
    protected $result; // 发送成功或失败
    protected $type; // 邮件类型 0注册 其它
    public function __construct()
    {
        $this->type = 1;
        $this->result = 0;
        $this->code = '';
    }

    public function sendEmail($email, $object, $body)
    {
        try {
            $mail = new PHPMailer(true);
            $mail->isSMTP(); // 启用SMTP
            $mail->SMTPDebug = SMTP::DEBUG_OFF; //开启调试模式
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
            $mail->CharSet = 'utf-8'; //设置邮件编码格式
            $mail->Host = "smtp.qq.com"; //smtp服务器的名称（这里以126邮箱为例）
            $mail->SMTPAuth = true; //启用smtp认证
            $mail->Username = "5294059@qq.com"; //你的邮箱名可以不写@后缀，也可以写
            $mail->Password = "foxkzcilstoycbbg"; //邮箱密码，现在开启邮箱SMTP后叫做安全码
            // 5294059 foxkzcilstoycbbg  dxs1232007 TNKYEZZUDTHIOOAA
            $mail->Port = 465; //SMTP端口号
            $mail->setFrom("5294059@qq.com", "河南海腾"); //发件人地址（也就是你的邮箱地址）和发件人名称
            $mail->AddAddress($email, ""); //接收人地址和名称
            $mail->WordWrap = 100; //设置每行字符长度
            $mail->isHTML(true); // 是否HTML格式邮件
            $mail->Subject = $object; //邮件主题
            $mail->Body = $body; //邮件内容
            //$mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端"; //邮件正文不支持HTML的备用显示
            $result = $mail->Send(); //发送方法，发送成功返回true，失败返回false
            //echo $mail->ErrorInfo; //获取错误信息
            $data = [
                'code' => 1,
                'msg' => '发送成功'
            ];
            $this->result = 1;
        } catch (Exception $e) {
            $data = [
                'code' => 0,
                'msg' => $mail->ErrorInfo
            ];
            $this->result = 0;
        }
        return $data;
    }

    public function sendYzEmail($email)
    {
        if (empty($email)) {
            return ['code'=>0, 'msg'=>'请输入邮箱'];
        }
        $EmailCode = self::getCode();
        $this->body =  '【河南海腾】您的验证码为 ' . $EmailCode . ' 请在30分钟内使用。';
        $this->subject =  '河南海腾验证服务';
        $this->email = $email;
        return self::sendEmail($email,$this->subject, $this->body);
    }

    public function saveSendLog()
    {
        $param['username'] = $this->username;
        $param['code'] = $this->code;
        $param['send_type'] = !empty($this->code) ? 2 : 1 ; //1 通知无code 2验证带code
        $param['body'] = $this->body;
        $param['flag'] = $this->flag;
        $param['email'] = $this->email;
        $param['subject'] = $this->subject;
        $param['success'] = $this->result;
        $param['type'] = $this->type;
        $param['is_sms'] = 'email';
        SmsLogModel::create($param);
    }

    // 验证code
    public function checkEmailCode($email,$flag,$code)
    {
        $where = [
            'email' => $email,
            'flag' => $flag,
            'code' => $code,
            'status'=> 0
        ];
        $log = SmsLogModel::where($where)->find();
        if ($log) {
            return true;
        } else {
            return false;
        }
    }

    // 设置用户名
    public function setUserName($username)
    {
        $this->username = $username;
    }
    // 获取验证码
    public function getCode()
    {
        if (empty($this->code)) {
            self::setCode();
        }
        return $this->code;
    }
    // 设置验证码
    public function setCode($code = null)
    {
        if (empty($code)) {
            $this->code = Str::random(4,1);
        } else {
            $this->code = $code;
        }
    }
    // 设置随机字条串
    public function setFlag($flag)
    {
        $this->flag = $flag;
    }
    public function getFlag()
    {
        return $this->flag;
    }
    // 设置邮件类型
    public function setType($type)
    {
        $this->type = $type;
    }
    public function getType()
    {
        return $this->type;
    }
}